import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        if (!queue1.isEmpty()) {
            queue1.offer(x);
        } else {
            queue2.offer(x);
        }
    }

    public int pop() {
        if(empty()){
            return -1;
        }

        if(!queue1.isEmpty()){
            int size = queue1.size();
            while(size-1!=0){
                queue2.offer(queue1.poll());
                size--;
            }
            return queue1.poll();
        }else if(!queue2.isEmpty()){
            int size = queue2.size();
            while(size-1!=0){
                queue1.offer(queue2.poll());
                size--;
            }
            return queue2.poll();
        }
        return -1;
    }

    public int top() {
        if(empty()){
            return -1;
        }

        if(!queue1.isEmpty()){
            int size = queue1.size();
            int temp=-1;
            while(size!=0){
                temp=queue1.poll();
                queue2.offer(temp);
                size--;
            }
            return temp;
        }else if(!queue2.isEmpty()){
            int size = queue2.size();
            int temp=-1;
            while(size!=0){
                temp=queue2.poll();
                size--;
                queue1.offer(temp);

            }
            return temp;
        }
        return -1;
    }

    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }
}